/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/


void
hanoiWindow::init()
{
    this->hanoi = new torreMalditona(3);
   
    for (int counter = 1; counter <= 3; counter ++)
    {
	std::string name = "ring";
	
	for( int i = 0; i < counter; i++)
	    name.push_back('.');
	
	QLabel* 
	newRing = new QLabel( this );
	
	newRing->resize(20*counter, 20);
	newRing->name(name.c_str());
	newRing->setBackgroundMode(Qt::PaletteDark  );
	newRing->setText( "anel" );

	this->rings.push_back(newRing);
    }
    
    this->updatePins();
}

void 
hanoiWindow::destroy()
{
    delete this->hanoi;	
}
void hanoiWindow::updateRingsLcd()
{
    this->ringsLcd->display( this->ringSlider->value() );
}

void hanoiWindow::setRestartConditions()
{
    this->ringsgroup->setEnabled( true );
}


void hanoiWindow::restart()
{
    this->rings.clear();

    const unsigned short int boga = this->ringSlider->value();
   
    this->hanoi->restartGame( boga );
    
    for (int counter = 1; counter <= boga; counter ++)
    {
	QLabel* 
	newRing = new QLabel(this, "RING");
	
	newRing->resize(20*counter, 20);
	
	this->rings.push_back(newRing);
    }
    
    this->updatePins();
}


void hanoiWindow::setFristPin()
{
    if ( this->BtnFirstPin->isOn() )
    {
	if ( this->BtnSecPin->isOn() )
	{
	    this->realocateRing(1,2);
	}
	else if ( this->BtnThirdPin->isOn() )
	{
	    this->realocateRing(1,3);
	}
	this->updatePins();
    }
}


void hanoiWindow::setSecPin()
{
    if ( this->BtnSecPin->isOn() )
    {
	if ( this->BtnFirstPin->isOn() )
	{
	    this->realocateRing(2,1);
	}
	else if ( this->BtnThirdPin->isOn() )
	{
	    this->realocateRing(2,3);
	}
	this->updatePins();
    }
    
 
}   

void hanoiWindow::setThirdPin()
{
    if ( this->BtnThirdPin->isOn() )
    {
	if ( this->BtnSecPin->isOn() )
	{
	    this->realocateRing(3,2);
	}
	else if ( this->BtnFirstPin->isOn() )
	{
	    this->realocateRing(3,1);
	}
	
	this->updatePins();
    }
}


void hanoiWindow::updatePins()
{
    if ( this->hanoi->getPinStats( 1 ).size()  > 0)
    {
	this->putRingsOnPin( this->hanoi->getPinStats( 1) , 1);
    }
    if ( this->hanoi->getPinStats(2).size() > 0)
    {
	this->putRingsOnPin(this->hanoi->getPinStats( 2), 2);
    }
    if ( this->hanoi->getPinStats(3).size() > 0 )
    {
	this->putRingsOnPin(this->hanoi->getPinStats( 3), 3);
    }
}	    


void hanoiWindow::realocateRing( const unsigned short int originPin , 
			        const unsigned short int destinationPin)
{
    try
    {
	this->hanoi->moveRing( destinationPin , originPin );
	
	this->BtnSecPin->setOn( false);
	this->BtnThirdPin->setOn(false);
	this->BtnFirstPin->setOn(false);
	
	this->updatePins();
	
	if ( this->hanoi->checkWin() )
	{
	    QMessageBox::information( this, "Application name", "Parabens!!! Voce ganhou!!!" );
	    
	    this->restart();
	}
    }
    catch (error* e)
    {
	QMessageBox::information( this, "Application name", e->report() );

	this->BtnSecPin->setOn( false);
	this->BtnThirdPin->setOn(false);
	this->BtnFirstPin->setOn(false);	
    }
}


void hanoiWindow::putRingsOnPin( std::vector<unsigned short int> pinVector, unsigned short int pin)
{
    int 
    correctX;
    
    int
    correctY;

    int
    intCounter = 0;
    
    for( std::vector<unsigned short int>::iterator counter = pinVector.begin() ; counter != pinVector.end(); counter++)
    {
	correctX = 60 + 110 *(pin -1);
	correctY = 250 - 20 * intCounter;
	
	QLabel*
	ring = this->rings[*counter -1];

	ring->move( correctX, correctY );
	 
	intCounter++;
    }
}


